package Practica03;

import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Eje03 {
	
	public static void main(String[] args){
		MyVentana v = new MyVentana();
		v.Iniciar();
	}

}

class MyVentana extends JFrame implements ActionListener{
	
	private JLabel lblPrimero;
	private JTextField txtPrimero;
	private JLabel lblSegundo;
	private JTextField txtSegundo;
	private JLabel lblRes;
	private JTextField txtRes;
	
	private JButton btnCalcular;

	private ButtonGroup grupo;
	private JRadioButton rbSum;
	private JRadioButton rbRes;
	private JRadioButton rbMul;
	private JRadioButton rbDiv;
	
	private static final long serialVersionUID = 1L;

	public MyVentana(){
		super("Claculadora Basica");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
		
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
		
		p.add(GenerarLinea1());
		p.add(GenerarLinea3());
		add(p);
		add(GenerarLinea2());
		add(GenerarLinea4());
	}
	
	private JPanel GenerarLinea1(){
		JPanel tmp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		lblPrimero = new JLabel("Primer Numero:");
		txtPrimero = new JTextField(20);
		
		tmp.add(lblPrimero);
		tmp.add(txtPrimero);
		return tmp;
	}
	
	private JPanel GenerarLinea2(){
		JPanel tmp = new JPanel(new FlowLayout(FlowLayout.LEFT));
		rbSum = new JRadioButton("Sumar");
		rbSum.setMnemonic(0);
		rbSum.setSelected(true);
		rbRes = new JRadioButton("Restar");
		rbRes.setMnemonic(1);
		rbMul = new JRadioButton("Multiplicar");
		rbMul.setMnemonic(2);
		rbDiv = new JRadioButton("Dividir");
		rbDiv.setMnemonic(3);
		
		grupo = new ButtonGroup();
		grupo.add(rbSum);
		grupo.add(rbRes);
		grupo.add(rbMul);
		grupo.add(rbDiv);
		
		tmp.add(rbSum);
		tmp.add(rbRes);
		tmp.add(rbMul);
		tmp.add(rbDiv);
		
		return tmp;
	}
	
	private JPanel GenerarLinea3(){
		JPanel tmp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		lblSegundo = new JLabel("Segundo Numero:");
		txtSegundo = new JTextField(20);
		
		tmp.add(lblSegundo);
		tmp.add(txtSegundo);
		return tmp;
	}
	
	private JPanel GenerarLinea4(){
		JPanel tmp = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblRes = new JLabel("Resultado:");
		txtRes = new JTextField(20);
		txtRes.setEditable(false);
		btnCalcular = new JButton("Calcular");
		btnCalcular.addActionListener(this);
		
		tmp.add(lblRes);
		tmp.add(txtRes);
		tmp.add(btnCalcular);
		return tmp;
	}
	
	public void Iniciar(){
		pack();
		setResizable(false);
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		Point pt = ge.getCenterPoint();
		setLocation(pt.x-250, pt.y-250);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		int a,b;
		String men="";
		boolean flag=false;
		
		try{
			a = Integer.parseInt(txtPrimero.getText());
		}catch (NumberFormatException ex){
			a=0;
			txtPrimero.setText(Integer.toString(a));
			men+="Error en el primer numero.\n";
			flag=true;
		}
		
		try{
			b = Integer.parseInt(txtSegundo.getText());
		}catch (NumberFormatException ex){
			b=0;
			txtSegundo.setText(Integer.toString(b));
			men+="Error en el segundo numero.\n";
			flag=true;
		}
		
		if(flag){
			JOptionPane.showMessageDialog(this, men, "Error", JOptionPane.WARNING_MESSAGE);
		}
		else{
			switch(grupo.getSelection().getMnemonic()){
			case 0:
				txtRes.setText(Integer.toString(a+b));
				break;
			case 1:
				txtRes.setText(Integer.toString(a-b));
				break;
			case 2:
				txtRes.setText(Integer.toString(a*b));
				break;
			case 3:
				if(b!=0){
				txtRes.setText(Integer.toString(a/b));
				}else{
					JOptionPane.showMessageDialog(this, "El segundo numero no puede ser 0", "Error", JOptionPane.WARNING_MESSAGE);
				}
				break;
			default:
				break;
			}
		}
	}
	
}